/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Rewriting;


import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author dimartinomirko
 */
public class PermutationsGenerator<E> {
    
    public List<List<E>> generatePerm(List<E> original) {
        List<E> originalcopy = new ArrayList<E>(original);
     if (originalcopy.size() == 0) { 
       List<List<E>> result = new ArrayList<List<E>>();
       result.add(new ArrayList<E>());
       return result;
     }
     E firstElement = originalcopy.remove(0);
     List<List<E>> returnValue = new ArrayList<List<E>>();
     List<List<E>> permutations = generatePerm(originalcopy);
     for (List<E> smallerPermutated : permutations) {
       for (int index=0; index <= smallerPermutated.size(); index++) {
         List<E> temp = new ArrayList<E>(smallerPermutated);
         temp.add(index, firstElement);
         returnValue.add(temp);
       }
     }
     return returnValue;
   }
    
}
